// index.js
// 获取应用实例
const app = getApp()

var {getIndexBanner,getIndexGoods} = require("../../api/index.js");//导入接口数据
var {getCategory} = require("../../api/category.js");
Page({
  data: {
    tipsflag:false,
    images:[
      '../../images/banner01.jpg',
      '../../images/banner02.jpg',
      '../../images/banner03.jpg',
      '../../images/banner04.jpg',
    ],
    navList:[
      {
        img:'../../images/icon-qiandao.png',
        text:'签到'
      },
      {
        img:'../../images/icon-fujin.png',
        text:'附近'
      },
      {
        img:'../../images/icon-zhanhui.png',
        text:'展会'
      },
      {
        img:'../../images/icon-fuli.png',
        text:'福利'
      },
      {
        img:'../../images/icon-muma.png',
        text:'玩乐'
      },
      {
        img:'../../images/icon-tiyu.png',
        text:'体育'
      },
      {
        img:'../../images/icon-dw.png',
        text:'周边'
      },
      {
        img:'../../images/icon-qinzi.png',
        text:'亲子'
      },

    ],
    goodsList:[
      {
        id:1,
        picUrl:'../../images/lists01.webp',
        description:'创维网络平板液晶电视机 100寸大屏电视机',
        address:'广东广州',
        oldPrice:4999,
        price:3989
      },
    ]
  },
  // 跳转分类页
  goCategory(options){
    let index =  options.currentTarget.dataset.index;
    // 向数据缓存中存储数据
    wx.setStorageSync('categoryIndex', index);
    // wx.switchTab tabbar中方法无法传参
    wx.switchTab({
      url: '/pages/category/category',
    })
},
// 跳转搜索页
goSearch(){
  wx.navigateTo({
    url: '/pages/search/search',
  })
},
    // 加入购物车
    addCart(data){
    let id = data.currentTarget.dataset.id;
    let goodsindex = this.data.goodsList.findIndex(item=>item.id == id)
     // 购物车数据结构
     let cartLists ={
      id:data.currentTarget.dataset.id,
      picUrl:this.data.goodsList[goodsindex].picUrl,
      description:this.data.goodsList[goodsindex].description,
      price:this.data.goodsList[goodsindex].price,
      oldPrice:this.data.goodsList[goodsindex].oldPrice,
      num:1,
      }
      // 数据添加数据缓存中
      // 1.判断数据缓存中是否有数据
      let goodsCarList = wx.getStorageSync("goodsCarlist");//获取数据
      // 2.有数据
      if(goodsCarList){
          // 2.1有相同数据，数据加一
          let index = goodsCarList.findIndex(item=>item.id == data.currentTarget.dataset.id)
          if(index != -1){  //有相同数据
              goodsCarList[index].num += 1;
          }else{
              // 2.2有数据没有相同数据,添加数据
              goodsCarList.push(cartLists);
          }
          wx.setStorageSync("goodsCarlist",goodsCarList);
      }else{
          // 3.没有数据 添加数据
          wx.setStorageSync("goodsCarlist",[cartLists]);
      }
      this.setData({
        tipsflag:true,
      })
      setTimeout(() => {
        this.setData({
          tipsflag:false,
        })
      }, 1500);
    },
  // 事件处理函数
  bindViewTap() {
  
  },
  /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      // 请求轮播图数据
      getIndexBanner().then((data)=>{
        this.setData({
          images:data.message
        })
      })
      // 请求首页导航栏数据
      getCategory().then(data=>{
         let name = data.message.slice(0,8)
         name.forEach((item,index)=>{
          let text = "navList["+index+"].text"
           this.setData({
            [text]:item.cat_name
           })
         })
      })
    // 请求商品数据
    getIndexGoods().then((data)=>{
      let lists = [];
      let goodslists = data.message.goods;
      goodslists.forEach(item=>{
          let {goods_id,goods_small_logo,goods_name,goods_price} = item;
          let data = {
              id:goods_id,
              picUrl:goods_small_logo,
              description:goods_name,
              address:"广州",
              price:goods_price,
              oldPrice:goods_price+100
          };
          if(goods_small_logo){
              lists.push(data);
          }
      })
      this.setData({
          goodsList:lists
      })
  })

    },
  getUserProfile(e) {
   
  },
  getUserInfo(e) {
  
  }
})
